package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.SaleOrder;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.query.SaleOrderQuery;
import com.banksoft.cps.bean.warpper.SaleOrderWarpper;
import com.banksoft.cps.bean.warpper.TransportCompanyWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.Process;
import com.banksoft.cps.service.SaleOrderService;
import com.banksoft.cps.service.TransportCompanyService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import com.banksoft.cps.util.UserContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/4.
 */
@Controller
@RequestMapping("/admin/saleOrder/")
public class AdminSaleOrderController extends ControllerConstants {
    @Autowired
    SaleOrderService saleOrderService;
    @Autowired
    TransportCompanyService transportCompanyService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(SaleOrderQuery query){
        List<SaleOrderWarpper> list =  saleOrderService.listWarpper(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(SaleOrderQuery query){
        long count =  saleOrderService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        SaleOrderWarpper warpper = saleOrderService.findWarpperById(id);
        return toJSONString(createDataTip(warpper));
    }
    @RequestMapping(value = "pullData",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String pullData(String id){
        try {
            saleOrderService.pullData(id);
        }catch (Exception e){
            return toExceptionString(e);
        }
        return toJSONString(SUCCESS);
    }
    @RequestMapping(value = "remark", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String remark(SaleOrder bean, HttpServletRequest request){
        try {
            //只要审核失败过一次。就标识
            if(Process.auditFail.name().equals(bean.getProcess())){
                bean.setRefuseFlag("1");
            }
            saleOrderService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }


    @RequestMapping(value = "deepWarpper",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deepWarpper(String id){
        SaleOrderWarpper warpper = saleOrderService.findWarpperById(id);
        if(warpper !=null && warpper.getTransportCompany() != null &&
                StringUtils.isNotBlank( warpper.getTransportCompany().getCompanyId())){
            TransportCompanyWarpper transportCompanyWarpper = transportCompanyService.
                    findWarpperById( warpper.getTransportCompany().getId());
            warpper.setTransportCompany(transportCompanyWarpper);
        }
        return toJSONString(createDataTip(warpper));
    }
    @RequestMapping(value = "delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String ids){
         if(StringUtil.isNullOrEmpty(ids)){
             return toJSONString(new ErrorTip(1,"请选择要删除的记录"));
         }
         saleOrderService.delete(ids.split(","));
         return toJSONString(SUCCESS);
    }
}
